package cn.com.winning.ecare.activity;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;

import yxt.push.org.androidpn.client.Constants;
import yxt.push.org.androidpn.client.NotificationDetailsActivity;
import yxt.push.org.androidpn.client.ServiceManager;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.TextView;
import android.widget.Toast;
import cn.com.winning.ecare.common.AsyncImageLoader;
import cn.com.winning.ecare.common.Grideviewmenu;
import cn.com.winning.ecare.common.MyApplication;
import cn.com.winning.ecare.utils.HTTPGetTool;
import cn.com.winning.ecare.utils.URLUtils;
import cn.com.winning.ecare.widgets.BidirSlidingLayout;
import cn.com.winning.ecare.widgets.CustomProgressDialog;

import com.easemob.chatuidemo.activity.LoginActivity;

/**
 * 主界面
 * 
 * @author Administrator
 */
public class Mainmean extends Activity
{

	private static final int WHAT_DID_LOAD_DATA = 0;
	private static final int WHAT_DID_LOAD_DATA_STATE = 1;
	private static final int WHAT_DID_LOAD_DATA_PHONE = 2;
	private Mainmean oThis = this;
	MyApplication myApp;
	private ViewPager viewPager; // android-support-v4中的滑动组件
	private List<ImageView> imageViews = new ArrayList<ImageView>(); // 滑动的图片集合
	private String[] titles; // 图片标题
	private int[] imageResId = new int[] { R.drawable.bg1, R.drawable.bg2, R.drawable.bg3 }; // 图片ID
	private String[] imageRes = new String[] { "bg1.jpg", "bg2.jpg", "bg3.jpg" }; // 图片ID
	private List<View> dots; // 图片标题正文的那些点
	private TextView tv_title;
	private int currentItem = 0; // 当前图片的索引号
	// An ExecutorService that can schedule commands to run after a given delay,
	// or to execute periodically.
	private ScheduledExecutorService scheduledExecutorService;
	Grideviewmenu gm;
	private GridView gridView;
	CustomProgressDialog proDialog;// 加载框
	MyAdapter adapter = null;
	private ImageButton jz_view = null;
	AssetManager am = null;
	/**
	 * 侧滑布局对象，用于通过手指滑动将左侧的菜单布局进行显示或隐藏。
	 */
	private BidirSlidingLayout slidingLayout;

	// private LinearLayout content;

	/**
	 * menu按钮，点击按钮展示左侧布局，再点击一次隐藏左侧布局。
	 */
	private ImageButton menuButton;
	private ImageButton mainmean_yygh;
	private ImageButton jkcp_view;

	private ImageView mainmean_report = null;
	private ImageView mainmean_path = null;
	private ImageView mainmean_disease = null;
	private ImageView mainmean_hospital = null;
	private ImageView mainmean_nearby = null;
	private ImageView mainmean_navigation = null;
	private ImageView mainmean_satisfaction = null;
	private ImageView mainmean_suggestion = null;
	private ImageView mainmean_settings = null;
	private ImageView mainmean_cost = null;
	private ImageView mainmean_food = null;
	private ImageView mainmean_shopping = null;
	private ImageView mainmean_costall = null;
	private ImageView mainmean_costmz = null;
	private ImageView mainmean_queueup = null;
	private ImageView mainmean_yllt = null;
	private ImageView mainmean_tjbg = null;
	// 住院历史记录
	List<Map<String, Object>> list_zy = new ArrayList<Map<String, Object>>();
	private String sfzh = "";
	private TextView nametv;
	private String gnbms = "";
	private String deviceid;

	public ServiceManager serviceManager;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{

		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		this.setContentView(R.layout.mainmean);
		myApp = (MyApplication) getApplication();
		myApp.addActivity(this);
		SharedPreferences sharedPrefs = getSharedPreferences(Constants.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE);
		deviceid = sharedPrefs.getString(Constants.DEVICE_ID, "");
		// myApp.getUserRecords().setModule("Mainmean");
		// UserRecord.getInstance(myApp.getUserRecords()).init();
		dots = new ArrayList<View>();
		dots.add(findViewById(R.id.v_dot0));
		dots.add(findViewById(R.id.v_dot1));
		dots.add(findViewById(R.id.v_dot2));
		findView();
		initGn();
		setOnClick();
		viewPager = (ViewPager) findViewById(R.id.vp);

		viewPager.setOnPageChangeListener(new MyPageChangeListener());
		slidingLayout = (BidirSlidingLayout) findViewById(R.id.bidir_sliding_layout);
		menuButton = (ImageButton) findViewById(R.id.more_view);
		// content = (LinearLayout) findViewById(R.id.content);
		// 将监听滑动事件绑定在contentListView上
		// slidingLayout.setScrollEvent(menuButton);
		menuButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				if (slidingLayout.isRightLayoutVisible())
				{
					slidingLayout.scrollToContentFromRightMenu();
				}
				else
				{
					slidingLayout.scrollToRightMenu();
				}
			}
		});
		nametv = (TextView) findViewById(R.id.mainmenuusername_menu);
		nametv.setText(myApp.getHzxm());
		TextView tv = (TextView) findViewById(R.id.mainmenuout_menu);
		tv.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				finish();
				Intent intent = new Intent();
				intent.setClass(oThis, Login.class);
				startActivity(intent);
			}
		});

		TextView familychoice = (TextView) findViewById(R.id.familychoice_menu);
		familychoice.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent();
				intent.setClass(oThis, UserChoiceActivity.class);
				startActivity(intent);
			}
		});
		slidingLayout.setScrollEvent(jz_view);
		slidingLayout.setScrollEvent(mainmean_yygh);
		slidingLayout.setScrollEvent(menuButton);
		slidingLayout.setScrollEvent(jkcp_view);

		GetData();

		// 连上医院服务器
		// 实例化serviceManager，主要是从androidpn.properties获取到一些配置信息，然后放到SharedPreferences里
		if (myApp.getServiceManager() != null)
		{
			serviceManager = myApp.getServiceManager();
			serviceManager.stopThread();
			serviceManager.stopService();
		}
		else
		{
			serviceManager = new ServiceManager(oThis);
		}
		// 设置推送的图标，还是把图标的ID放到SharedPreferences里
		serviceManager.setNotificationIcon(R.drawable.notification);
		// startService,跳转到org.androidpn.client.NotificationService里执行
		serviceManager.startService();
		myApp.setServiceManager(serviceManager);
	}

	/**
	 * 初始化功能列表，默认全部
	 * 膳食查询ssqk,一日清单yrqd,检查检验报告jybg,临床路径lclj,疾病百科jkzs,满意度调查myddc,意见建议yjx,
	 * 医院信息yyxx,自选超市zxcs,院内导航yndh,就诊助手jzzs,预约挂号yygh,医疗费用ylfy
	 */
	private void initGn()
	{
		Intent intent = getIntent();
		gnbms = intent.getStringExtra("gnbm");
	}

	private void GetData()
	{
		proDialog = CustomProgressDialog.createDialog(oThis);
		proDialog.setMessage("数据加载中...");
		proDialog.show();
		proDialog.setCanceledOnTouchOutside(false);
		// 这里单开一个线程和后台数据交互
		new Thread(new InitData_()).start();
		// 这里单开一个线程和后台数据交互 获取用户是否与医院信息匹配
		new Thread(new InitUserState()).start();
		// 这里单开一个线程和后台数据交互 修改登入用户手机信息到消息系统
		new Thread(new ChangeUserInfo()).start();

	}

	private Handler myHandler = new Handler()
	{

		@Override
		public void handleMessage(Message msg)
		{
			super.handleMessage(msg);
			switch (msg.what)
			{
				case WHAT_DID_LOAD_DATA:
				{
					if (proDialog != null && proDialog.isShowing())
					{
						proDialog.dismiss();
					}
					imageViews = (List<ImageView>) msg.obj;
					if (adapter == null)
					{
						adapter = new MyAdapter();
					}
					viewPager.setAdapter(adapter);
					adapter.notifyDataSetChanged();// 更新
					scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
					// 当Activity显示出来后，每两秒钟切换一次图片显示
					scheduledExecutorService.scheduleAtFixedRate(new ScrollTask(), 2, 5, TimeUnit.SECONDS);
					break;
				}
				case WHAT_DID_LOAD_DATA_STATE:
				{
					if (proDialog != null && proDialog.isShowing())
					{
						proDialog.dismiss();
					}
					nametv.setText(myApp.getHzxm());
					String string = (String) msg.obj;
					if ("1".equals(string))
					{
						nametv.setTextColor(Color.parseColor("#818181"));
					}
					else
					{
						nametv.setTextColor(getResources().getColor(R.color.blue_bg));
					}
					break;
				}

				case WHAT_DID_LOAD_DATA_PHONE:
				{
					if (proDialog != null && proDialog.isShowing())
					{
						proDialog.dismiss();
					}
					break;
				}

			}

		}

	};

	class InitData_ implements Runnable
	{

		@Override
		public void run()
		{

			Looper.prepare();
			initData();
			Looper.loop();
		}

	}

	class InitUserState implements Runnable
	{

		@Override
		public void run()
		{

			Looper.prepare();
			initUser();
			Looper.loop();
		}

	}

	class ChangeUserInfo implements Runnable
	{

		@Override
		public void run()
		{

			Looper.prepare();
			changeUserPhone();
			Looper.loop();
		}

	}

	private void initData()
	{
		try
		{
			Message msg = myHandler.obtainMessage();
			Intent intent = getIntent();
			String yyimg = intent.getStringExtra("yyimg");

			String[] yyimageRes = yyimg.split(",");

			final List<ImageView> templist = new ArrayList<ImageView>();
			ImageView imageView = null;
			if (yyimg.equals(""))
			{
				// 初始化图片资源,后台没设置，用默认图片
				for (int i = 0; i < imageResId.length; i++)
				{
					imageView = new ImageView(oThis);
					imageView.setImageResource(imageResId[i]);
					imageView.setScaleType(ScaleType.CENTER_CROP);
					templist.add(imageView);
					imageView = null;
				}
			}
			else
			{
				for (int i = 0; i < yyimageRes.length; i++)
				{
					imageView = new ImageView(oThis);
					AsyncImageLoader imageLoader = new AsyncImageLoader(getApplicationContext());
					Bitmap bitmap = imageLoader.loadImageS(yyimageRes[i], URLUtils.HOST + URLUtils.HOSPITALPICMAIN);

					if (bitmap != null)
					{
						imageView.setImageBitmap(bitmap);
					}
					imageView.setScaleType(ScaleType.CENTER_CROP);
					templist.add(imageView);
					imageView = null;
				}
			}
			Log.e("2", "" + templist.size());
			// if (templist.size() == 3)
			// {
			msg.what = WHAT_DID_LOAD_DATA;
			msg.obj = templist;
			myHandler.sendMessage(msg);
			// }

		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	private void initUser()
	{
		try
		{
			Message msg = myHandler.obtainMessage();
			List<NameValuePair> ls = new ArrayList<NameValuePair>();
			ls.add(new BasicNameValuePair("realname", myApp.getHzxm()));
			ls.add(new BasicNameValuePair("pcard", myApp.getSfzh()));
			ls.add(new BasicNameValuePair("phoneno", myApp.getSjh()));

			JSONObject json = HTTPGetTool.getTool().post(URLUtils.HOST + URLUtils.URLGETUSERSTATE, ls);
			JSONArray jsonArr;
			if (json != null && !json.equals(""))
			{
				if (!json.toString().equals("{\"msg\":\"系统错误！\",\"success\":\"false\"}"))
				{
					if (json.getString("rigisterstate").length() > 0)
					{
						// myApp.setZhstate(json.getString("rigisterstate"));
						msg.obj = json.getString("rigisterstate");
					}
					else
					{
						// myApp.setZhstate("1");
						msg.obj = "1";
					}
				}
				else
				{
					// myApp.setZhstate("1");
					msg.obj = "1";
				}
			}
			else
			{
				// myApp.setZhstate("1");
				msg.obj = "1";
			}
			msg.what = WHAT_DID_LOAD_DATA_STATE;
			myHandler.sendMessage(msg);
		}
		catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
			Message msg = myHandler.obtainMessage();
			msg.what = -1;
			msg.obj = e.toString();
			myHandler.sendMessage(msg);
		}
	}

	private void changeUserPhone()
	{
		try
		{
			Message msg = myHandler.obtainMessage();
			List<NameValuePair> ls = new ArrayList<NameValuePair>();
			String state = "";
			ls.add(new BasicNameValuePair("deviceid", deviceid));
			ls.add(new BasicNameValuePair("mobilephone", myApp.getSjh()));
			ls.add(new BasicNameValuePair("accountname", myApp.getAccount()));
			JSONObject json = HTTPGetTool.getTool().post(URLUtils.HOST + URLUtils.CHANGEPHONE, ls);
			JSONArray jsonArr;
			if (json != null)
			{
				if (!json.toString().equals("{\"msg\":\"系统错误！\",\"success\":\"false\"}"))
				{

				}
				else
				{
					msg.obj = "1";
				}
			}
			else
			{
				msg.obj = "1";
			}
			msg.what = WHAT_DID_LOAD_DATA_PHONE;
			myHandler.sendMessage(msg);
		}
		catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
			Message msg = myHandler.obtainMessage();
			msg.what = -1;
			msg.obj = e.toString();
			myHandler.sendMessage(msg);
		}
	}

	private Handler handler = new Handler()
	{
		public void handleMessage(android.os.Message msg)
		{
			viewPager.setCurrentItem(currentItem);// 切换当前显示的图片
		};
	};

	private void setOnClick()
	{
		mainmean_report.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				if (gnbms.indexOf(",jybg,") != -1)
				{// 检查检验报告
					// 重新获取下省份证号
					myApp = (MyApplication) getApplication();
					sfzh = myApp.getSfzh() == null ? "" : myApp.getSfzh();
					if (sfzh.equals(""))
					{
						Toast.makeText(Mainmean.this, "请先选择家庭成员", Toast.LENGTH_SHORT).show();
						Intent intent = new Intent();
						intent.setClass(oThis, UserChoiceActivity.class);
						startActivity(intent);
					}
					else
					{
						Intent intent = new Intent(oThis, ReportActivity.class);
						startActivity(intent);
					}
				}
				else
				{
					Toast.makeText(Mainmean.this, "医院未开通此功能权限！", Toast.LENGTH_SHORT).show();
				}

			}

		});
		mainmean_path.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				if (gnbms.indexOf(",lclj,") != -1)
				{// 临床路径
					Intent intent = new Intent(oThis, ClinicPathOne.class);
					startActivity(intent);
				}
				else
				{
					Toast.makeText(Mainmean.this, "医院未开通此功能权限！", Toast.LENGTH_SHORT).show();
				}
			}
		});
		mainmean_disease.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				if (gnbms.indexOf(",jkzs,") != -1)
				{// 疾病百科
					Intent intent = new Intent(oThis, Health1.class);
					startActivity(intent);
				}
				else
				{
					Toast.makeText(Mainmean.this, "医院未开通此功能权限！", Toast.LENGTH_SHORT).show();
				}
			}
		});
		mainmean_hospital.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				if (gnbms.indexOf(",yyxx,") != -1)
				{// 医院信息
					Intent intent = new Intent(oThis, HospitalOne.class);
					startActivity(intent);
				}
				else
				{
					Toast.makeText(Mainmean.this, "医院未开通此功能权限！", Toast.LENGTH_SHORT).show();
				}
			}
		});
		mainmean_nearby.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent(oThis, BaiduMapActivity.class);
				startActivity(intent);
				// Toast.makeText(Mainmean.this, "医院未开通此功能权限！",
				// Toast.LENGTH_SHORT).show();
			}
		});
		mainmean_navigation.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				if (gnbms.indexOf(",yndh,") != -1)
				{// 院内导航
					Intent intent = new Intent(oThis, Yndh.class);
					startActivity(intent);
				}
				else
				{
					Toast.makeText(Mainmean.this, "医院未开通此功能权限！", Toast.LENGTH_SHORT).show();
				}
			}
		});
		mainmean_satisfaction.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				if (gnbms.indexOf(",myddc,") != -1)
				{// 满意度调查
					Intent intent = new Intent(oThis, Process.class);
					startActivity(intent);
				}
				else
				{
					Toast.makeText(Mainmean.this, "医院未开通此功能权限！", Toast.LENGTH_SHORT).show();
				}
			}
		});
		mainmean_suggestion.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				if (gnbms.indexOf(",yjx,") != -1)
				{// 意见建议
					Intent intent = new Intent(oThis, Suggestion.class);
					startActivity(intent);
				}
				else
				{
					Toast.makeText(Mainmean.this, "医院未开通此功能权限！", Toast.LENGTH_SHORT).show();
				}
			}
		});
		mainmean_settings.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{

				Intent intent = new Intent(oThis, AccountManage.class);
				startActivity(intent);
			}
		});
		mainmean_cost.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				if (gnbms.indexOf(",yrqd,") != -1)
				{// 一日清单
					myApp = (MyApplication) getApplication();
					sfzh = myApp.getSfzh() == null ? "" : myApp.getSfzh();
					if (sfzh.equals(""))
					{
						Toast.makeText(Mainmean.this, "请先选择家庭成员", Toast.LENGTH_SHORT).show();
						Intent intent = new Intent();
						intent.setClass(oThis, UserChoiceActivity.class);
						startActivity(intent);
					}
					else
					{
						Intent intent = new Intent(oThis, Costeveryday.class);
						startActivity(intent);
					}
				}
				else
				{
					Toast.makeText(Mainmean.this, "医院未开通此功能权限！", Toast.LENGTH_SHORT).show();
				}
			}
		});
		mainmean_food.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				if (gnbms.indexOf(",ssqk,") != -1)
				{// 膳食查询
					myApp = (MyApplication) getApplication();
					sfzh = myApp.getSfzh() == null ? "" : myApp.getSfzh();
					if (sfzh.equals(""))
					{
						Toast.makeText(Mainmean.this, "请先选择家庭成员", Toast.LENGTH_SHORT).show();
						Intent intent = new Intent();
						intent.setClass(oThis, UserChoiceActivity.class);
						startActivity(intent);
					}
					else
					{
						Intent intent = new Intent(oThis, SSSelect.class);
						startActivity(intent);
					}
				}
				else
				{
					Toast.makeText(Mainmean.this, "医院未开通此功能权限！", Toast.LENGTH_SHORT).show();
				}
			}
		});
		mainmean_shopping.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				if (gnbms.indexOf(",zxcs,") != -1)
				{// 自选超市
					myApp = (MyApplication) getApplication();
					sfzh = myApp.getSfzh() == null ? "" : myApp.getSfzh();
					if (sfzh.equals(""))
					{
						Toast.makeText(Mainmean.this, "请先选择家庭成员", Toast.LENGTH_SHORT).show();
						Intent intent = new Intent();
						intent.setClass(oThis, UserChoiceActivity.class);
						startActivity(intent);
					}
					else
					{
						Intent intent = new Intent(oThis, Shop.class);
						startActivity(intent);
					}
				}
				else
				{
					Toast.makeText(Mainmean.this, "医院未开通此功能权限！", Toast.LENGTH_SHORT).show();
				}
			}
		});

		jz_view.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				if (gnbms.indexOf(",jzzs,") != -1)
				{// 就诊信息
					myApp = (MyApplication) getApplication();
					sfzh = myApp.getSfzh() == null ? "" : myApp.getSfzh();
					if (sfzh.equals(""))
					{
						Toast.makeText(Mainmean.this, "请先选择家庭成员", Toast.LENGTH_SHORT).show();
						Intent intent = new Intent();
						intent.setClass(oThis, UserChoiceActivity.class);
						startActivity(intent);
					}
					else
					{
						Intent intent = new Intent(oThis, NotificationDetailsActivity.class);
						startActivity(intent);
					}
				}
				else
				{
					Toast.makeText(Mainmean.this, "医院未开通此功能权限！", Toast.LENGTH_SHORT).show();
				}
			}
		});
		mainmean_yygh.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				// TODO Auto-generated method stub
				if (gnbms.indexOf(",yygh,") != -1)
				{// 预约挂号
					myApp = (MyApplication) getApplication();
					sfzh = myApp.getSfzh() == null ? "" : myApp.getSfzh();
					if (sfzh.equals(""))
					{
						Toast.makeText(Mainmean.this, "请先选择家庭成员", Toast.LENGTH_SHORT).show();
						Intent intent = new Intent();
						intent.setClass(oThis, UserChoiceActivity.class);
						startActivity(intent);
					}
					else
					{
						// TODO Auto-generated method stub
						Intent intent = new Intent(oThis, GhOne.class);
						startActivity(intent);
					}
				}
				else
				{
					Toast.makeText(Mainmean.this, "医院未开通此功能权限！", Toast.LENGTH_SHORT).show();
				}
			}
		});
		mainmean_costall.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				// TODO Auto-generated method stub
				if (gnbms.indexOf(",ylfy,") != -1)
				{// 医疗费用
					myApp = (MyApplication) getApplication();
					sfzh = myApp.getSfzh() == null ? "" : myApp.getSfzh();
					if (sfzh.equals(""))
					{
						Toast.makeText(Mainmean.this, "请先选择家庭成员", Toast.LENGTH_SHORT).show();
						Intent intent = new Intent();
						intent.setClass(oThis, UserChoiceActivity.class);
						startActivity(intent);
					}
					else
					{
						// TODO Auto-generated method stub
						Intent intent = new Intent(oThis, CostSelect.class);
						startActivity(intent);
					}
				}
				else
				{
					Toast.makeText(Mainmean.this, "医院未开通此功能权限！", Toast.LENGTH_SHORT).show();
				}
			}
		});
		mainmean_costmz.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				// TODO Auto-generated method stub
				if (gnbms.indexOf(",mzyl,") != -1)
				{// 医疗费用
					myApp = (MyApplication) getApplication();
					sfzh = myApp.getSfzh() == null ? "" : myApp.getSfzh();
					if (sfzh.equals(""))
					{
						Toast.makeText(Mainmean.this, "请先选择家庭成员", Toast.LENGTH_SHORT).show();
						Intent intent = new Intent();
						intent.setClass(oThis, UserChoiceActivity.class);
						startActivity(intent);
					}
					else
					{
						// TODO Auto-generated method stub
						Intent intent = new Intent(oThis, CostMzSelect.class);
						startActivity(intent);
					}
				}
				else
				{
					Toast.makeText(Mainmean.this, "医院未开通此功能权限！", Toast.LENGTH_SHORT).show();
				}
			}
		});
		jkcp_view.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				// TODO Auto-generated method stub
				if (gnbms.indexOf(",jkcp,") != -1)
				{// 健康测评
					myApp = (MyApplication) getApplication();
					sfzh = myApp.getSfzh() == null ? "" : myApp.getSfzh();
					if (sfzh.equals(""))
					{
						Toast.makeText(Mainmean.this, "请先选择家庭成员", Toast.LENGTH_SHORT).show();
						Intent intent = new Intent();
						intent.setClass(oThis, UserChoiceActivity.class);
						startActivity(intent);
					}
					else
					{
						// TODO Auto-generated method stub
						Intent intent = new Intent(oThis, HealthAssessment.class);
						startActivity(intent);
					}
				}
				else
				{
					Toast.makeText(Mainmean.this, "医院未开通此功能权限！", Toast.LENGTH_SHORT).show();
				}

			}
		});

		mainmean_queueup.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				// TODO Auto-generated method stub
				if (gnbms.indexOf("pdcx") != -1)
				{// 健康测评
					myApp = (MyApplication) getApplication();
					sfzh = myApp.getSfzh() == null ? "" : myApp.getSfzh();
					if (sfzh.equals(""))
					{
						Toast.makeText(Mainmean.this, "请先选择家庭成员", Toast.LENGTH_SHORT).show();
						Intent intent = new Intent();
						intent.setClass(oThis, UserChoiceActivity.class);
						startActivity(intent);
					}
					else
					{
						// TODO Auto-generated method stub
						Intent intent = new Intent(oThis, QueueUp.class);
						startActivity(intent);
					}
				}
				else
				{
					Toast.makeText(Mainmean.this, "医院未开通此功能权限！", Toast.LENGTH_SHORT).show();
				}

			}
		});

		mainmean_yllt.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				if (gnbms.indexOf("yllt") != -1)
				{// 健康测评
					// TODO Auto-generated method stub
					Intent intent = new Intent(oThis, LoginActivity.class);
					startActivity(intent);
				}
				else
				{
					Toast.makeText(Mainmean.this, "医院未开通此功能权限！", Toast.LENGTH_SHORT).show();
				}

			}
		});
		mainmean_tjbg.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				if (gnbms.indexOf("tjbg") != -1)
				{
					myApp = (MyApplication) getApplication();
					sfzh = myApp.getSfzh() == null ? "" : myApp.getSfzh();
					if (sfzh.equals(""))
					{
						Toast.makeText(Mainmean.this, "请先选择家庭成员", Toast.LENGTH_SHORT).show();
						Intent intent = new Intent();
						intent.setClass(oThis, UserChoiceActivity.class);
						startActivity(intent);
					}
					else
					{
						Intent intent = new Intent(oThis, TjbgActivity.class);
						startActivity(intent);
					}

				}
				else
				{
					Toast.makeText(Mainmean.this, "医院未开通此功能权限！", Toast.LENGTH_SHORT).show();
				}

			}
		});

	}

	private void findView()
	{
		mainmean_report = (ImageView) findViewById(R.id.mainmean_report);
		mainmean_path = (ImageView) findViewById(R.id.mainmean_path);
		mainmean_disease = (ImageView) findViewById(R.id.mainmean_disease);
		mainmean_hospital = (ImageView) findViewById(R.id.mainmean_hospital);
		mainmean_nearby = (ImageView) findViewById(R.id.mainmean_nearby);
		mainmean_navigation = (ImageView) findViewById(R.id.mainmean_navigation);
		mainmean_satisfaction = (ImageView) findViewById(R.id.mainmean_satisfaction);
		mainmean_suggestion = (ImageView) findViewById(R.id.mainmean_suggestion);
		mainmean_settings = (ImageView) findViewById(R.id.mainmean_settings);
		mainmean_cost = (ImageView) findViewById(R.id.mainmean_cost);
		mainmean_food = (ImageView) findViewById(R.id.mainmean_food);
		mainmean_shopping = (ImageView) findViewById(R.id.mainmean_shopping);
		mainmean_costall = (ImageView) findViewById(R.id.mainmean_costall);
		mainmean_costmz = (ImageView) findViewById(R.id.mainmean_costmz);
		mainmean_queueup = (ImageView) findViewById(R.id.mainmean_queueup);
		mainmean_yllt = (ImageView) findViewById(R.id.mainmean_yllt);
		mainmean_tjbg = (ImageView) findViewById(R.id.mainmean_tjbg);
		jz_view = (ImageButton) findViewById(R.id.jz_view);
		mainmean_yygh = (ImageButton) findViewById(R.id.yy_view);
		jkcp_view = (ImageButton) findViewById(R.id.jkcp_view);
	}

	@Override
	protected void onStart()
	{
		// scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
		// // 当Activity显示出来后，每两秒钟切换一次图片显示
		// scheduledExecutorService.scheduleAtFixedRate(new ScrollTask(), 2, 5,
		// TimeUnit.SECONDS);
		super.onStart();
	}

	@Override
	protected void onStop()
	{
		// 当Activity不可见的时候停止切换
		if (scheduledExecutorService != null)
		{
			scheduledExecutorService.shutdown();
		}
		super.onStop();
	}

	/**
	 * 换行切换任务
	 * 
	 * @author Administrator
	 */
	private class ScrollTask implements Runnable
	{

		public void run()
		{
			synchronized (viewPager)
			{
				currentItem = (currentItem + 1) % imageViews.size();
				handler.obtainMessage().sendToTarget(); // 通过Handler切换图片
			}
		}

	}

	/**
	 * 当ViewPager中页面的状态发生改变时调用
	 * 
	 * @author Administrator
	 */
	private class MyPageChangeListener implements OnPageChangeListener
	{
		private int oldPosition = 0;

		/**
		 * This method will be invoked when a new page becomes selected. position:
		 * Position index of the new selected page.
		 */
		public void onPageSelected(int position)
		{
			currentItem = position;
			dots.get(oldPosition % dots.size()).setBackgroundResource(R.drawable.dot_normal);
			dots.get(position % dots.size()).setBackgroundResource(R.drawable.dot_focused);
			oldPosition = position;
		}

		public void onPageScrollStateChanged(int arg0)
		{

		}

		public void onPageScrolled(int arg0, float arg1, int arg2)
		{

		}
	}

	/**
	 * 填充ViewPager页面的适配器
	 * 
	 * @author Administrator
	 */
	private class MyAdapter extends PagerAdapter
	{

		@Override
		public int getCount()
		{
			return imageResId.length;
		}

		@Override
		public Object instantiateItem(View arg0, int arg1)
		{
			((ViewPager) arg0).addView(imageViews.get(arg1 % imageViews.size()), arg1);
			return imageViews.get(arg1 % imageViews.size());
		}

		@Override
		public void destroyItem(View arg0, int arg1, Object arg2)
		{
			((ViewPager) arg0).removeView((View) arg2);
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1)
		{
			return arg0 == arg1;
		}

		@Override
		public void restoreState(Parcelable arg0, ClassLoader arg1)
		{

		}

		@Override
		public Parcelable saveState()
		{
			return null;
		}

		@Override
		public void startUpdate(View arg0)
		{

		}

		@Override
		public void finishUpdate(View arg0)
		{

		}
	}

	// 返回键监听
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)
	{
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0)
		{
			oThis.finish();
			return true;
		}
		return super.onKeyUp(keyCode, event);
	}

	@Override
	protected void onResume()
	{
		// TODO Auto-generated method stub
		super.onResume();
		String flag = myApp.getActiveFlag() == null ? "" : myApp.getActiveFlag();
		if (null != flag && !flag.equals(""))
		{
			new Thread(new InitUserState()).start();
			new Thread(new ChangeUserInfo()).start();
			myApp.setActiveFlag("");
		}
	}

}
